package com.tian.clothing.util;

import com.tian.clothing.R;
import com.tian.clothing.global.ClothingApplication;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @ClassName: DialogUtil
 * @Description: 对话框帮助类
 * @author: dreamfutureba@gmail.com
 * @date: 2013年11月25日 下午4:22:35
 */
public class DialogUtil {
	private Context context;
	private Dialog baseDialog;

	private Dialog progressDialog;
	private TextView progressDialog_msg;
	private ImageView progressDialog_icon;

	// 构造函数
	public DialogUtil(Context context) {
		super();
		this.context = context;
	}
	
	/**
	 * 创建对话框
	 * @param dialogStyle    对话框样式
	 * @param dialogView     对话框布局
	 * @param isTouchCancel  是否点击取消
	 * @param dialogGravity  对话框显示位置
	 */
	public Dialog createDialog(int dialogStyle,int dialogView,boolean isTouchCancel,int dialogGravity)
	{
		baseDialog=new Dialog(context,dialogStyle);
		baseDialog.setContentView(dialogView);
		baseDialog.setCanceledOnTouchOutside(isTouchCancel);
		return baseDialog;
	}
	
	/**
	 * 显示进度条对话框
	 * @param title    标题
	 * @param msg      显示信息
	 * @param icon     显示图标
	 */
	public void showProgressDialog(String msg){
		progressDialog=createDialog(R.style.progress_dialog, R.layout.progress_dialog, false, Gravity.TOP);
		progressDialog_msg=(TextView) baseDialog.findViewById(R.id.progressDialog_msg);
		progressDialog_icon=(ImageView) baseDialog.findViewById(R.id.progressDialog_icon);
		
		progressDialog_msg.setText(msg);
		
		Animation animation =AnimationUtils.loadAnimation(context, R.anim.progress_dialog);
		progressDialog_icon.setAnimation(animation);
		
		progressDialog.show();
	}
	/**
	 * 关闭进度条对话框
	 */
	public void closeProgressgDialog()
	{
		if(progressDialog.isShowing())
		{
			progressDialog.dismiss();
		}
	}
	
	/**
	 * 显示一个按钮的对话框
	 * @param context    上下文
	 * @return AlertDialog
	 */
	
	public static AlertDialog showOneBtnDialog(final Context context,String title,String msg){
		Resources resources = context.getResources();
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(msg);
		builder.setNegativeButton(resources.getString(R.string.confirm), null);
		return builder.create();
	}
	
	/**
	 * 显示两个按钮的对话框
	 * @param context    上下文
	 * @return AlertDialog
	 */
	
	public static AlertDialog showTwoBtnDialog(final Context context,String title,
			String msg,String confirm,String cancle,
			OnClickListener confirmListener,OnClickListener cancleListener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(msg);
		builder.setPositiveButton(confirm, confirmListener);
		builder.setNegativeButton(cancle, cancleListener);
		return builder.create();
	}
	

	/**
	 * 退出对话框
	 * @param context    上下文
	 * @return AlertDialog
	 */
	public static AlertDialog newExitDialog(final Context context){
		Resources resources = context.getResources();
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(resources.getString(R.string.tip));
		builder.setMessage(resources.getString(R.string.app_exit_tip));
		builder.setPositiveButton(resources.getString(R.string.exit),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						ClothingApplication.getApp().exitApp();
					}
				});
		builder.setNegativeButton(resources.getString(R.string.cancel), null);
		return builder.create();
	}
}
